﻿#include "TextEditPropertyEditEntry.h"
#include <QTextEdit>

#include "BooleanEditEntry.h"
#include "EnumEditEntry.h"
#include "IntegerEditEntry.h"
#include "FlagsEditEntry.h"
#include "UiTextEditEntry.h"
#include "DoubleEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(TextEdit)

struct TextEditPropertyEditEntry::TextEditPropertyEditEntryPrivate {
    QPointer<QTextEdit> object;

    QPointer<FlagsEditEntry> entryAutoFormating;
    QPointer<BooleanEditEntry> entryTabChangesFocus;
    QPointer<UiTextEditEntry> entryDocumentTitle;
    QPointer<BooleanEditEntry> entryUndoRedoEnabled;
    QPointer<EnumEditEntry> entryLineWrapMode;
    QPointer<IntegerEditEntry> entryLineWrapColumnOrWidth;
    QPointer<BooleanEditEntry> entryReadOnly;
    QPointer<UiTextEditEntry> entryMarkdown;
    QPointer<UiTextEditEntry> entryHtml;
    QPointer<BooleanEditEntry> entryOverwriteMode;
    QPointer<DoubleEditEntry> entryTabStopDistance;
    QPointer<BooleanEditEntry> entryAcceptRichText;
    QPointer<IntegerEditEntry> entryCursorWidth;
    QPointer<FlagsEditEntry> entryTextInteractionFlags;
    QPointer<UiTextEditEntry> entryPlaceHolderText;
};

TextEditPropertyEditEntry::~TextEditPropertyEditEntry() { }

TextEditPropertyEditEntry::TextEditPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TextEditPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void TextEditPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QTextEdit*>(object);
}

void TextEditPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryAutoFormating->setValue(p->object->autoFormatting());
    p->entryTabChangesFocus->setValue(p->object->tabChangesFocus());
    p->entryDocumentTitle->setValue(p->object->documentTitle());
    p->entryUndoRedoEnabled->setValue(p->object->isUndoRedoEnabled());
    p->entryLineWrapMode->setValue(p->object->lineWrapMode());
    p->entryLineWrapColumnOrWidth->setValue(p->object->lineWrapColumnOrWidth());
    p->entryReadOnly->setValue(p->object->isReadOnly());
    p->entryMarkdown->setValue(p->object->toMarkdown());
    p->entryHtml->setValue(p->object->toHtml());
    p->entryOverwriteMode->setValue(p->object->overwriteMode());
    p->entryTabStopDistance->setValue(p->object->tabStopDistance());
    p->entryAcceptRichText->setValue(p->object->acceptRichText());
    p->entryCursorWidth->setValue(p->object->cursorWidth());
    p->entryTextInteractionFlags->setValue(p->object->textInteractionFlags());
    p->entryPlaceHolderText->setValue(p->object->placeholderText());
}

void TextEditPropertyEditEntry::initUi() {
    p->entryAutoFormating = new FlagsEditEntry;
    p->entryAutoFormating->setMetaEnum(QMetaEnum::fromType<QTextEdit::AutoFormatting>());
    appendChildEntry(p->entryAutoFormating);

    p->entryTabChangesFocus = new BooleanEditEntry;
    appendChildEntry(p->entryTabChangesFocus);

    p->entryDocumentTitle = new UiTextEditEntry;
    appendChildEntry(p->entryDocumentTitle);

    p->entryUndoRedoEnabled = new BooleanEditEntry;
    appendChildEntry(p->entryUndoRedoEnabled);

    p->entryLineWrapMode = new EnumEditEntry;
    p->entryLineWrapMode->setMetaEnum(QMetaEnum::fromType<QTextEdit::LineWrapMode>());
    appendChildEntry(p->entryLineWrapMode);

    p->entryLineWrapColumnOrWidth = new IntegerEditEntry;
    appendChildEntry(p->entryLineWrapColumnOrWidth);

    p->entryReadOnly = new BooleanEditEntry;
    appendChildEntry(p->entryReadOnly);

    p->entryMarkdown = new UiTextEditEntry;
    appendChildEntry(p->entryMarkdown);

    p->entryHtml = new UiTextEditEntry;
    appendChildEntry(p->entryHtml);

    p->entryOverwriteMode = new BooleanEditEntry;
    appendChildEntry(p->entryOverwriteMode);

    p->entryTabStopDistance = new DoubleEditEntry;
    appendChildEntry(p->entryTabStopDistance);

    p->entryAcceptRichText = new BooleanEditEntry;
    appendChildEntry(p->entryAcceptRichText);

    p->entryCursorWidth = new IntegerEditEntry;
    appendChildEntry(p->entryCursorWidth);

    p->entryTextInteractionFlags = new FlagsEditEntry;
    p->entryTextInteractionFlags->setMetaEnum(QMetaEnum::fromType<Qt::TextInteractionFlag>());
    appendChildEntry(p->entryTextInteractionFlags);

    p->entryPlaceHolderText = new UiTextEditEntry;
    appendChildEntry(p->entryPlaceHolderText);
}

void TextEditPropertyEditEntry::retranslateUi() {
    setTitle(tr("QTextEdit"));
    p->entryAutoFormating->setTitle(tr("autoFormating"));
    p->entryTabChangesFocus->setTitle(tr("tabChangesFocus"));
    p->entryDocumentTitle->setTitle(tr("documentTitle"));
    p->entryUndoRedoEnabled->setTitle(tr("undoRedoEnabled"));
    p->entryLineWrapMode->setTitle(tr("lineWrapMode"));
    p->entryLineWrapColumnOrWidth->setTitle(tr("lineWrapColumnOrWidth"));
    p->entryReadOnly->setTitle(tr("readOnly"));
    p->entryMarkdown->setTitle(tr("markdown"));
    p->entryHtml->setTitle(tr("html"));
    p->entryOverwriteMode->setTitle(tr("overwriteMode"));
    p->entryTabStopDistance->setTitle(tr("tabStopDistance"));
    p->entryAcceptRichText->setTitle(tr("acceptRichText"));
    p->entryCursorWidth->setTitle(tr("cursorWidth"));
    p->entryTextInteractionFlags->setTitle(tr("textInteractionFlags"));
    p->entryPlaceHolderText->setTitle(tr("placeHolderText"));
}
